//
//  IGTopicDetailCommentCell.m
//  jingdongfang
//
//  Created by BOE on 2017/9/4.
//  Copyright © 2017年 LJL. All rights reserved.
//

#import "IGTopicDetailCommentCell.h"
#import "IntroViewController.h"

@interface IGTopicDetailCommentCell ()

@property (nonatomic, strong) UIImageView *headerimgView;
@property (nonatomic, strong) UILabel *nameLabel;                             ///< 头像右侧的姓名
@property (nonatomic, strong) UILabel *dateLabel;                             ///< 头像右侧的日期
@property (nonatomic, strong) YYLabel *contentLabel;
@property (nonatomic, strong) NSMutableAttributedString *commentAttributedText;///< 评论富文本信息

@end

@implementation IGTopicDetailCommentCell

+ (IGTopicDetailCommentCell *)cellWithTableView:(UITableView *)tableView {
    IGTopicDetailCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:kIGTopicDetailCommentCell];
    if (!cell) {
        cell = [[IGTopicDetailCommentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kIGTopicDetailCommentCell];
        cell.contentView.backgroundColor = [UIColor c3Color];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        self.headerimgView = [[UIImageView alloc] init];
        self.headerimgView.layer.masksToBounds = YES;
        self.headerimgView.layer.cornerRadius = 15;
        [self.contentView addSubview:self.headerimgView];
        self.headerimgView.backgroundColor = [UIColor mainBackgroundColor];
        
        self.nameLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.nameLabel];
        self.nameLabel.numberOfLines = 1;
        self.nameLabel.textColor = [UIColor c4Color];
        self.nameLabel.font = [UIFont font18];
        
        self.dateLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.dateLabel];
        self.dateLabel.numberOfLines = 1;
        self.dateLabel.textColor = [UIColor c5Color];
        self.dateLabel.font = [UIFont font12];
        
        // 评论内容
        self.contentLabel = [[YYLabel alloc] init];
        [self.contentView addSubview:self.contentLabel];
        self.contentLabel.backgroundColor = [UIColor clearColor];
        self.contentLabel.preferredMaxLayoutWidth = KSCREENWIDTH - 5 * kGAP10 - 2*kGAP10;
        self.contentLabel.lineBreakMode = NSLineBreakByCharWrapping;
        self.contentLabel.numberOfLines = 0;
        self.contentLabel.displaysAsynchronously = YES;
        self.contentLabel.textColor = [UIColor c4Color];
        self.contentLabel.font = [UIFont font13];

        [self.headerimgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(kGAP15);
            make.top.mas_equalTo(kGAP20);
            make.width.height.mas_equalTo(30);
        }];
        
        [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.headerimgView.mas_right).offset(kGAP10);
            make.centerY.mas_equalTo(self.headerimgView);
            make.height.mas_equalTo(18);
        }];
        
        [self.dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.nameLabel.mas_right).offset(kGAP10);
            make.centerY.mas_equalTo(self.headerimgView);
            make.height.mas_equalTo(18);
        }];
        
        [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.headerimgView.mas_bottom).offset(14);
            make.left.mas_equalTo(self.headerimgView.mas_right).offset(kGAP10);
            make.right.mas_equalTo(-kGAP15);
        }];
        
        UIView *lineView = [[UIView alloc] init];
        lineView.backgroundColor = [UIColor mainBackgroundColor];
        [self.contentView addSubview:lineView];
        
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(0);
            make.height.mas_equalTo(1);
            make.bottom.mas_equalTo(0);
        }];
        
        self.backgroundColor = [UIColor clearColor];
        self.hyb_lastViewInCell = self.contentLabel;
        self.hyb_bottomOffsetToCell = 3.0;
    }
    
    return self;
}

- (void)configCellWithModel:(IGCommentBean *)model {
    //清空
    self.commentAttributedText = [[NSMutableAttributedString alloc] initWithString:@""];
    
    [self.headerimgView sd_setImageWithURL:[NSURL URLWithString:model.commentUserAvater] placeholderImage:[UIImage imageNamed:@"place_head_Icon"]];
    self.headerimgView.userInteractionEnabled = YES;
    [self.headerimgView setTapActionWithBlock:^{
        IntroViewController *introVC = [IntroViewController new];
        introVC.u_id = model.commentUserId;
        [UIViewController ig_pushViewController:introVC animated:YES];
    }];
    self.nameLabel.text = model.commentUserNick;
    self.dateLabel.text = [NSDate timestampToString:model.dateCreated Format:@"MM-dd HH:mm"];
    self.nameLabel.userInteractionEnabled = YES;
    [self.nameLabel setTapActionWithBlock:^{
        IntroViewController *introVC = [IntroViewController new];
        introVC.u_id = model.commentUserId;
        [UIViewController ig_pushViewController:introVC animated:YES];
    }];

    UIFont *font13 = [UIFont font13];
    UIColor *color5 = [UIColor c5Color];
    if (![model.parentCommentUserId isEqualToString:@""] && model.parentCommentUserId != nil) {//二级回复
        NSMutableAttributedString *commentInfo = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@ %@：%@", NSLocalizedString(@"Reply",nil),model.parentCommentUserNick, model.content] attributes:@{NSFontAttributeName: font13}];
        
        NSString *language = [[NSUserDefaults standardUserDefaults] objectForKey:KLanguage];
        NSUInteger localAddress;
        if ([language containsString:@"zh-Hans"]) {
            localAddress = 3;
        } else {
            localAddress = 6;
        }
        [commentInfo yy_setTextHighlightRange:NSMakeRange(localAddress, model.parentCommentUserNick.length+1) color:color5 backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
            IntroViewController *introVC = [IntroViewController new];
            introVC.u_id = model.parentCommentUserId;
            [UIViewController ig_pushViewController:introVC animated:YES];
        }];
        
        [self.commentAttributedText appendAttributedString:commentInfo];
    } else {//直接回复
        NSMutableAttributedString *commentInfo = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@", model.content] attributes:@{NSFontAttributeName: font13}];
        
        [self.commentAttributedText appendAttributedString:commentInfo];
    }

    self.commentAttributedText.yy_lineSpacing = 5;
    self.commentAttributedText.yy_kern = [NSNumber numberWithFloat:.5];
    self.contentLabel.attributedText = self.commentAttributedText;
}

- (NSMutableAttributedString *)commentAttributedText {
    if (!_commentAttributedText) {
        _commentAttributedText = [NSMutableAttributedString new];
    }
    return _commentAttributedText;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
